package com.js.foodordersystem.controller;

import com.alibaba.fastjson.JSONObject;
import com.js.foodordersystem.entity.Dish;
import com.js.foodordersystem.entity.Order;
import com.js.foodordersystem.entity.OrderItem;
import com.js.foodordersystem.entity.Payment;
import com.js.foodordersystem.service.DishService;
import com.js.foodordersystem.service.OrderItemService;
import com.js.foodordersystem.service.OrderService;
import com.js.foodordersystem.service.PaymentService;
import com.js.foodordersystem.util.JwtUtil;
import com.js.foodordersystem.util.Result;

import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * 存储订单信息(Order)表控制层
 *
 * @author makejava
 * @since 2025-01-09 10:52:50
 */
@Transactional(rollbackFor = Exception.class)
@RestController
@RequestMapping("order")
public class OrderController {
    /**
     * 服务对象
     */
    @Resource
    private OrderService orderService;
    @Resource
    private DishService dishService;
    @Resource
    private PaymentService paymentService;
    @Resource
    private OrderItemService orderItemService;
    /**
     * 分页查询
     *
     * @param order 筛选条件
     * @param limit 分页对象
     * @param page  分页对象
     * @return 查询结果
     */
    @GetMapping("findAll")
    public Result findAll(Order order, Integer limit, Integer page) {
        if (limit == null || page == null) {
            return Result.failure("limit和page不能为空");
        }
        return orderService.selectAll(order, limit, page);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("getById")
    public Result getById(Long id) {
        return Result.success(this.orderService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param order 实体
     * @return 新增结果
     */
    @PostMapping("add")
    public Result add(@RequestBody Order order, HttpServletRequest request) {
        //判断是否为空
        if (order == null) {
            return Result.failure("参数不能为空");
        }
        Dish dish = dishService.getById(order.getDishId());
        Long id = JwtUtil.getUserIdFromToken(request.getHeader("token"));
        //生成订单
        order.setUserId(id);
        order.setTotalPrice(dish.getPrice().multiply(new BigDecimal(order.getQuantity())));
        orderService.save(order);

        //生成订单详情
        OrderItem orderItem = new OrderItem();
        orderItem.setOrderId(order.getOrderId());
        orderItem.setDishId(order.getDishId());
        orderItem.setQuantity(order.getQuantity());
        orderItem.setPrice(dish.getPrice());
        orderItem.setTotalPrice(order.getTotalPrice());
        orderItemService.save(orderItem);

        //生成付款信息
        Payment payment = new Payment();
        payment.setOrderId(order.getOrderId());
        payment.setPaymentMethod(order.getPaymentMethod());
        payment.setPaymentStatus(10);
        payment.setPaymentTime(null);
        paymentService.save(payment);

        return Result.failure("添加失败");
    }

    /**
     * 编辑数据
     *
     * @param order 实体
     * @return 编辑结果
     */
    @PostMapping("edit")
    public Result edit(@RequestBody Order order) {
        //判断是否为空
        if (order == null) {
            return Result.failure("参数不能为空");
        }

        if (this.orderService.updateById(order)) {
            return Result.success("修改成功");
        }
        return Result.failure("修改失败");
    }

    /**
     * 删除数据
     *
     * @return 删除是否成功
     */
    @PostMapping("delete")
    public Result deleteById(@RequestBody JSONObject data) {
        //判断id是否为空
        Long id = data.getLong("orderId");
        if (id == null) {
            return Result.failure("id不能为空");
        }

        if (this.orderService.removeById(id)) {
            return Result.success("删除成功");
        }
        return Result.failure("删除失败");
    }

}

